var prefix = jsctx + "/hlhtTenantOpenPile";

var hlhtTenantOpenPile = (function () {
    function init() {
        load();
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageHlhtTenantOpenPile",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            elecStationId:$("#elecStationId").val(),
                            hlhtTenantCode:$("#hlhtTenantCode").val(),
                            hlhtOperatorId:$("#hlhtOperatorId").val(),
                            hlhtOpenType:$("#hlhtOpenType").val(),
                            pileName:$("#pileName").val(),
                            pileType:$("#pileType").val(),
                            isOpen:$("#isOpen").val(),
                            epHlhtOpenforType:$("#epHlhtOpenforType").val(),
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                        };
                    },
                    columns: [
                        {
                            checkbox: true, field: '', width: '50', align: 'left',
                        },
                        {
                            title: '#', field: '', width: '50',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '所属桩群', width: '100', align: 'left'},
                        {field: 'power', title: '额定功率', width: '100', align: 'left'},
                        {
                            field: 'hasParking', title: '是否有车位', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span>是</span>'
                                } else if (value == '1') {
                                    return '<span>否</span>'
                                }
                            }
                        },
                        {
                            field: 'hasLock', title: '是否有地锁', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span>否</span>'
                                } else if (value == '1') {
                                    return '<span>是</span>'
                                }
                            }
                        },
                        {field: 'pileNo', title: '桩编号', width: '100', align: 'left'},
                        {field: 'pileName', title: '桩名称', width: '100', align: 'left'},
                        {
                            field: 'pileElecType', title: '电桩类型', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '1') {
                                    return '<span>单直流</span>'
                                } else if (value == '2') {
                                    return '<span>单交流</span>'
                                } else if (value == '3') {
                                    return '<span>双直流</span>'
                                } else if (value == '4') {
                                    return '<span>双交流</span>'
                                }
                            }
                        },
                        {
                            field: 'pileType', title: '设备类型', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '1') {
                                    return '<span>直流设备</span>'
                                } else if (value == '2') {
                                    return '<span>交流设备</span>'
                                } else if (value == '3') {
                                    return '<span>交直流一体设备</span>'
                                } else if (value == '4') {
                                    return '<span>无线设备</span>'
                                } else if (value == '5') {
                                    return '<span>其他</span>'
                                }
                            }
                        },
                        {
                            field: '', title: '电枪型号', width: '450', align: 'left',
                            formatter: function (value, row, index) {
                                var z = '';

                                for (var i = 0; i < row.elecGunList.length; i++) {

                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);

                                    if (lastNum == '1') {
                                        var a = row.elecGunList[i].sortNum + '.';
                                        var b = "A型号电枪";

                                        var c = '<a class="btn btn-success btn-xs ' + has_edit + '" href="#" title="枪详情"  data-target="#myModal" data-toggle="modal"  onclick="hlhtTenantOpenPile.detailElecGun(\''
                                            + row.elecGunList[i].elecGunId
                                            + '\')">枪详情</a> ';

                                        if (row.elecGunList[i].operateStatus == '1') {
                                            var d = '<a class="btn btn-danger btn-xs ' + has_add + '" href="#" title="隐藏" onclick="pile.updateGunStatus(\''
                                                + row.elecGunList[i].elecGunId + '\')">隐藏</a> ';
                                        } else if (row.elecGunList[i].operateStatus == '2') {
                                            var d = '<a class="btn btn-info btn-xs ' + has_add + '" href="#" title="显示" onclick="pile.updateGunStatus(\''
                                                + row.elecGunList[i].elecGunId + '\')">显示</a> ';
                                        }

                                        var e = '<a class="btn btn-success btn-xs ' + has_edit + '" href="#" title="解锁"  data-target="#myModal" data-toggle="modal"  onclick="hlhtTenantOpenPile.sendUnlock(\''
                                            + row.elecGunList[i].elecGunId
                                            + '\')">解锁</a> ';

                                        var q = '<a class="btn btn-primary btn-xs ' + has_edit + '" href="#" title="二维码"   data-target="#myModal" data-toggle="modal"  onclick="hlhtTenantOpenPile.printElecGunQrCode(\''
                                            + row.elecGunList[i].gunNo
                                            + '\')">二维码</a> ';

                                        z += a + ' ' + b + ' ' + c + ' ' + d + ' ' + '' + e + '' + q + '<br>';
                                    } else if (lastNum == '2') {
                                        var a = row.elecGunList[i].sortNum + '.';
                                        var b = "B型号电枪";

                                        var c = '<a class="btn btn-success btn-xs ' + has_edit + '" href="#" title="枪详情"  data-target="#myModal" data-toggle="modal"  onclick="pile.detailElecGun(\''
                                            + row.elecGunList[i].elecGunId
                                            + '\')">枪详情</a> ';

                                        if (row.elecGunList[i].operateStatus == '1') {
                                            var d = '<a class="btn btn-danger btn-xs ' + has_add + '" href="#" title="隐藏" onclick="pile.updateGunStatus(\''
                                                + row.elecGunList[i].elecGunId + '\')">隐藏</a> ';
                                        } else if (row.elecGunList[i].operateStatus == '2') {
                                            var d = '<a class="btn btn-info btn-xs ' + has_add + '" href="#" title="显示" onclick="pile.updateGunStatus(\''
                                                + row.elecGunList[i].elecGunId + '\')">显示</a> ';
                                        }

                                        var e = '<a class="btn btn-success btn-xs ' + has_edit + '" href="#" title="解锁"  data-target="#myModal" data-toggle="modal"  onclick="hlhtTenantOpenPile.sendUnlock(\''
                                            + row.elecGunList[i].elecGunId
                                            + '\')">解锁</a> ';

                                        var q = '<a class="btn btn-primary btn-xs ' + has_edit + '" href="#" title="二维码"   data-target="#myModal" data-toggle="modal"  onclick="hlhtTenantOpenPile.printElecGunQrCode(\''
                                            + row.elecGunList[i].gunNo
                                            + '\')">二维码</a> ';

                                        z += a + ' ' + b + ' ' + c + ' ' + d + ' ' + '' + e + '' + q;
                                    }
                                }
                                return z;
                            }
                        },
                        {
                            field: '', title: '使用状态', width: '75', align: 'center',
                            formatter: function (value, row, index) {
                                var z = '';
                                for (var i = 0; i < row.elecGunList.length; i++) {
                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);
                                    if (lastNum == '1') {
                                        if (row.elecGunList[i].status == '0') {
                                            var d = '<a class="btn btn-info btn-xs " href="#">空闲</a> ';
                                        } else if (row.elecGunList[i].status == '1') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">使用中</a> ';
                                        } else if (row.elecGunList[i].status == '2') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">插抢中</a> ';
                                        }
                                        z += d + '<br>';
                                    } else if (lastNum == '2') {
                                        if (row.elecGunList[i].status == '0') {
                                            var d = '<a class="btn btn-info btn-xs " href="#">空闲</a> ';
                                        } else if (row.elecGunList[i].status == '1') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">使用中</a> ';
                                        } else if (row.elecGunList[i].status == '2') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">插抢中</a> ';
                                        }
                                        z += d;
                                    }
                                }
                                if (row.status == 0) {
                                    return '<a class="btn label-danger btn-xs " href="#">离线</a>'
                                } else {
                                    return z;
                                }
                            }
                        },
                        {
                            field: '', title: '电枪状态', width: '75', align: 'center',
                            formatter: function (value, row, index) {
                                var z = '';
                                for (var i = 0; i < row.elecGunList.length; i++) {
                                    var gunNoLastNum = row.elecGunList[i].gunNo;
                                    var lastNum = gunNoLastNum.substr(gunNoLastNum.length - 1, 1);
                                    if (lastNum == '1') {
                                        if (row.elecGunList[i].normalStatus == '0') {
                                            var d = '<a class="btn label-info btn-xs " href="#">正常</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '1') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">枪锁故障</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '255') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">故障</a> ';
                                        }
                                        z += d + '<br>';
                                    } else if (lastNum == '2') {
                                        if (row.elecGunList[i].normalStatus == '0') {
                                            var d = '<a class="btn label-info btn-xs " href="#">正常</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '1') {
                                            var d = '<a class="btn label-warning btn-xs " href="#">枪锁故障</a> ';
                                        } else if (row.elecGunList[i].normalStatus == '255') {
                                            var d = '<a class="btn label-danger btn-xs " href="#">故障</a> ';
                                        }
                                        z += d;
                                    }
                                }
                                return z;
                            }
                        },
                        {
                            field: 'openPileId', title: '是否开放',width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == null || value == '') {
                                    return '<span>否</span>'
                                } else {
                                    return '<span >是</span>'
                                }
                            }
                        },
                        {
                            field: 'epHlhtOpenforType', title: '是否支持第三方开放',width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span>是</span>'
                                } else if (value == '1') {
                                    return '<span >否</span>'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            width: '200',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="桩列表" onclick="hlhtTenantOpenPile.detailElecPile(\''
                                    + row.elecPileId
                                    + '\')">详情</a> ';
                                return a;

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function detailhlhtTenantOpen(tenantOpenId, isEdit) {
        location.href = prefix + "/detailHlhtTenantOpen?tenantOpenId=" + tenantOpenId + "&isEdit=" + isEdit;
    }

    function hlhtOrderInfo(tenantOpenId, tenantCode, hlhtTenantCode, hlhtOperatorId, hlhtType) {
        var id = "";
        if (hlhtTenantCode == null) {
            id = hlhtOperatorId;
        } else {
            id = hlhtTenantCode
        }
        location.href = prefix + "/hlhtOrderInfo?tenantOpenId=" + tenantOpenId + "&tenantCode=" + tenantCode + "&hlhtTenantCode=" + id + "&hlhtOperatorId=" + id
            + "&hlhtType=" + hlhtType;
    }

    function updateTenantOpenStatus(tenantOpenId, status) {
        $.ajax({
            url: prefix + "/updateHlhtTenantOpenStatus",
            type: "post",
            data: {
                'tenantOpenId': tenantOpenId,
                'status': status,
            },
            success: function (data) {

                if (data.status == 200) {
                    layer.msg("操作成功");
                    reLoad();
                } else {
                    layer.msg(data.message);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                layer.msg(textStatus + errorThrown);
            }
        });
    }

    function stationHlhtTenantOpen(tenantOpenId, tenantCode) {
        location.href = prefix + "/stationHlhtTenantOpen?tenantOpenId=" + tenantOpenId + "&tenantCode=" + tenantCode;
    }

    function pagePositiveHlhtStaion(tenantOpenId) {
        location.href = prefix + "/positiveHlhtStaion?tenantOpenId=" + tenantOpenId;
    }

    function goBack() {
        window.history.back();
    }

    function saveBatchHlhtTenantOpenStation(superLevelType) {
        layer.confirm('确定批量开放？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/saveBatchHlhtTenantOpenStation",
                type: "post",
                data: {
                    'superLevelType': superLevelType
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function isExpiredDate(date){
        var nowDate = new Date();
        var endDate = new Date(date);
        if (nowDate.getTime() > endDate.getTime()){
            return true;
        }
        return false;
    }

    function hlhtDiscount(hlhtOperatorId,tenantCode) {
        location.href = prefix + "/hlhtDiscount?hlhtOperatorId=" + hlhtOperatorId + "&tenantCode=" + tenantCode;
    }

    function detailElecPile(elecPileId) {
        location.href = jsctx + "/elecPile/detailElecPile/" + elecPileId;
    }

    function batchHandleRows() {
        var rows = $("#exampleTable").bootstrapTable('getSelections');//
        if (rows.length == 0) {
            alert("请先选择要设置的记录!");
            return;
        } else {
            var openPileIdList = new Array();
            $(rows).each(function () {
                if (this.openPileId != null){
                    openPileIdList.push(this.openPileId);
                }
            });
            var elecPileIdList = new Array();
            $(rows).each(function () {
                if (this.elecPileId != null){
                    elecPileIdList.push(this.elecPileId);
                }

            });
            if(openPileIdList.length==0){
                openPileIdList = null
            }
            if(elecPileIdList.length==0){
                elecPileIdList = null
            }
            layer.open({
                type: 1,
                title: '开放站点设置',
                closeBtn: false,
                shade: 0.4,
                shift: 7,
                shadeClose: true,
                content: $("#open-main"),
                btn: ['确定', '取消'],
                yes: function (index) {//layer.msg('yes');    //点击确定回调
                    var openStatus = $("input[name='openStatus']:checked").val();
                    var epHlhtOpenforType = $("input[name='epHlhtOpenforType']:checked").val();
                    var tenantOpenId = $("#tenantOpenId").val();

                    layer.close(index);
                    $.ajax({
                        url: prefix + "/updateOrSaveHlhtTenantOpenPile",
                        type: "post",
                        data: {
                            openPileIdList: openPileIdList,
                            elecPileIdList: elecPileIdList,
                            tenantOpenId: $("#tenantOpenId").val(),
                            openStatus: openStatus,
                            epHlhtOpenforType: epHlhtOpenforType

                        },
                        traditional:true,
                        success: function (data) {
                            layer.close(index);
                            if (data.status == 200) {
                                reLoad();
                                layer.msg(data.message);
                            } else {
                                layer.msg(data.message);
                            }
                        }
                    });
                },
            });
        }
    }

    function editElecGun(elecGunId) {
        location.href = jsctx + "/elecPile/editElecGun/" + elecGunId;
    }

    function detailElecGun(elecGunId) {
        location.href = jsctx + "/elecPile/detailElecGun/" + elecGunId;
    }

    function printElecGunQrCode(gunNo) {
        layer.open({
            type: 2,
            title: '枪号：' + gunNo,
            shadeClose: false,
            area: ['350px', '300px'],
            content: jsctx + "/elecPile/genElecGunQrCode/" + gunNo,
            //弹窗下方的按钮
            // btn: ['下载'],
            // //绑定第一个按钮的点击事件
            // btn1: function (gunNo) {
            //     genElecGunQrCodeUrl(gunNo)
            //     var url = document.getElementById("qrcode").childNodes[0].toDataURL("image/png");
            //     //下载图片到本地
            //     var a = document.createElement('a');
            //     var event = new MouseEvent('click');
            //     a.download = gunNo;
            //     a.href = url;
            //     a.dispatchEvent(event);
            // }
        });
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        detailElecPile:detailElecPile,
        batchHandleRows: batchHandleRows,
        printElecGunQrCode: printElecGunQrCode,
        detailElecGun: detailElecGun,
        stationHlhtTenantOpen: stationHlhtTenantOpen,
        saveBatchHlhtTenantOpenStation: saveBatchHlhtTenantOpenStation,
        hlhtDiscount: hlhtDiscount,
        pagePositiveHlhtStaion:pagePositiveHlhtStaion

    };
})();


